/* 
 * File:   MainWindow.cpp
 * Author: jose
 * 
 * Created on March 30, 2012, 6:13 PM
 */

#include "MainWindow.h"
#include "SdlUtils.h"

MainWindow::MainWindow() {
    //Open the font
    font = TTF_OpenFont("lazy.ttf", 28);
    click = CLICK_NONE;      

}

MainWindow::MainWindow(const MainWindow& orig) {
}

MainWindow::~MainWindow() {
}

bool MainWindow::loadFiles() {
    return true;
}

void MainWindow::handleEvents(SDL_Event *event) {

    //The mouse offsets
    int x = 0, y = 0;

    fontStylePvP = TTF_STYLE_NORMAL;
    fontStylePvC = TTF_STYLE_NORMAL;
    fontStyleEnd = TTF_STYLE_NORMAL;

    //If the mouse moved
    if (event->type == SDL_MOUSEMOTION) {
        //Get the mouse offsets
        x = event->motion.x;
        y = event->motion.y;

        if (y < 20) {
            fontStylePvP = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
        } else if (y > 80 && y < 120) {
            fontStylePvC = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
        } else if (y > 180 && y < 220) {
            fontStyleEnd = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
        }
    }
    //If a mouse button was pressed
    if (event->type == SDL_MOUSEBUTTONDOWN) {
        //If the left mouse button was pressed
        if (event->button.button == SDL_BUTTON_LEFT) {
            //Get the mouse offset
            x = event->motion.x;
            y = event->motion.y;

            if (y < 20) {
                fontStylePvP = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
            } else if (y > 80 && y < 120) {
                fontStylePvC = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
            } else if (y > 180 && y < 220) {
                fontStyleEnd = TTF_STYLE_BOLD | TTF_STYLE_ITALIC;
            }

            if (y < 20) {
                click = CLICK_PVP;
            } else if (y > 80 && y < 120) {
                click = CLICK_PVC;
            } else if (y > 180 && y < 220) {
                click = CLICK_END;
            }
      }
    }

}

void MainWindow::processLogic(GameStatus *gameStatus) {
    if (click != CLICK_NONE) {

        switch(click) {
            case CLICK_END:
                gameStatus->nextState = GameStatus::STATE_QUIT;
                break;
            case CLICK_PVP:
                gameStatus->nextState = GameStatus::STATE_PLAYING;
                gameStatus->numberOfPlayers = 2;
                break;
            case CLICK_PVC:
                gameStatus->nextState = GameStatus::STATE_PLAYING;
                gameStatus->numberOfPlayers = 1;
                break;
        }
        click = CLICK_NONE;
    }
}

void MainWindow::render(SDL_Surface *screen) {

    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));

    SDL_Color textColor = {255, 0, 0};



    TTF_SetFontStyle(font, fontStylePvP);
    txtPlayerVsPlayer = TTF_RenderText_Solid(font, "Player vs Player", textColor);
    TTF_SetFontStyle(font, fontStylePvC);
    txtPlayerVsCom = TTF_RenderText_Solid(font, "Player Vs Computer", textColor);
    TTF_SetFontStyle(font, fontStyleEnd);
    txtEndGame = TTF_RenderText_Solid(font, "End Game", textColor);

    SdlUtils::applySurface(0, 0, txtPlayerVsPlayer, screen);
    SdlUtils::applySurface(0, 100, txtPlayerVsCom, screen);
    SdlUtils::applySurface(0, 200, txtEndGame, screen);

}
